$(function () {
	//游戏名称
	window.gameName = 'climb-thegreatwall';
	//判断用户是否有抽奖(游戏)资格
	api.eligibleToPlay();
	//获取活动参数
	api.getGameConfig(gameName);
	//添加底部链接
	setFootLink('#startPage');

	//触摸点距离盒子的距离
	var box_x,box_y;
	//计时器
	var roadTimer,selfTimer;
	//全局计数变量
	var globalNum = 0;
	//捉多少鬼算成功
	var totalGuost = 20;
	//创建计时器
	var timer,moveTimer;
	//次数限制
	var limitNum = 3;
	//跑道初始top值
	var roadTop = 0;
	//跑道高度
	var roadBoxHeight = $('#gamePage').height();
	//剩余跑道总长度
	var restLength = 5403;
	//活动区域宽度
	var gameAreaWidth = $('#gameArea').width();
	//人的尺寸
	var person_w = $('#self').width();
	var person_h = $('#self').height();
	window.person2_w = $('.person[type="2"]').width();
	window.person2_h = $('.person[type="2"]').height();
	window.person3_w = $('.person[type="3"]').width();
	window.person3_h = $('.person[type="3"]').height();
	window.person5_w = $('.person[type="5"]').width();
	window.person5_h = $('.person[type="5"]').height();
	//点击[开始捉鬼]按钮
	$('#startBtn').on('click',function () {
		if (limitNum <= 0) {
			Toast('抽奖次数不够');
			return;
		}
		$('#startPage').hide();
		$('#rulePage').show();
		var countDown = 3;
		var tipTimer = setInterval(function  () {
			$('#countDown').text(--countDown);
			if (countDown === 0) {
				$('#rulePage').hide();
				$('#gamePage').show();
				clearInterval(tipTimer);
				gameBegin();
			}
		},1000);
	});

	

	//游戏开始
	function gameBegin() {
		//次数减少一次
		limitNum--;
		//初始化
		globalNum = 0;
		$('#gameArea').html('<div id="self"></div>');
		roadTop = 0;
		restLength = 5403;
		$('#roadbox').css('top',-roadBoxHeight);
		$('#restLength').text(restLength);
		roadTimer = setInterval(function  () {
			roadTop += 2;
			if (roadTop >= 0) {
				roadTop = -roadBoxHeight;
			};
			$('#roadbox').css('top',roadTop);
			$('#restLength').text(--restLength);
			if (restLength<=0) {
				clearInterval(roadTimer);
				$('#restLength').text(0);
				$('#successPage').show();
			};
			guostMove();
		}, 1000/60);
	};

	//移动事件
	function guostMove() {
		//定时生成鸡或者炸弹
		if (globalNum%90 === 0) {
			createItem();
		};
		var guosts = $('.person');
		var person_left = $('#self').position().left;
		var person_top = $('#self').position().top;
		guosts.map(function (ele, i) {
			var item_top = i.offsetTop;
			var item_left = i.offsetLeft;
			var item_w,item_h;
			var typeNum = $(i).attr('type');
			item_w = window['person'+typeNum+'_w'];
			item_h = window['person'+typeNum+'_h'];
			//清除已经超出边界的
			if (item_top > roadBoxHeight) {
				$(this).remove();
				return;
			} else {
				i.style.top = item_top + 2 + 'px';
			};
			//碰撞检测
			if (item_left+item_w >= person_left && item_left <= person_left+person_w) {
				if (item_top+item_h >= person_top && item_top <= person_top+person_h) {
					clearInterval(roadTimer);
					clearInterval(selfTimer);
					$('#self').addClass("getBomb");
					$('#failPage').fadeIn();
				};
			};
		});
		globalNum++;
	};

	//随机生成鸡或者炸弹
	function createItem() {
		//父盒子宽度5.9rem 人群最宽2.2rem;
		//left值随机范围 0 - 3.7rem;
		//产生随机数
		var num = (Math.random()*3.7).toFixed(1);
		var ranNum = Math.random();
		var typeNum = ranNum<0.33?'2':ranNum<0.66?'3':'5';
		var html = '<div class="person" type="'+typeNum+'" style="top:0;left:'+num+'rem"></div>';
		$('#gameArea').append(html);
	};

	//监听左方向键
	$('#leftbtn').on('touchstart',function(e){
		e.preventDefault();
		if ($('#self').hasClass('getBomb'))return;
		clearInterval(selfTimer);
		var left = $('#self').position().left;
		left -= 2;
		if (left < 0) {
			left = 0;
		};
		selfTimer = setInterval(function  () {
			left -= 2;
			if (left < 0) {
				left = 0;
			};
			$('#self').css('left', left);
		});
	});
	$('#leftbtn').on('touchend',function(e){
		e.preventDefault();
		clearInterval(selfTimer);
	});
	//监听右方向键
	$('#rightbtn').on('touchstart',function(e){
		e.preventDefault();
		if ($('#self').hasClass('getBomb'))return;
		clearInterval(selfTimer);
		var left = $('#self').position().left;
		left += 2;
		if (left > gameAreaWidth - person_w) {
			left =  gameAreaWidth - person_w;
		};
		selfTimer = setInterval(function  () {
			left += 2;
			if (left >  gameAreaWidth - person_w) {
				left =  gameAreaWidth - person_w;
			};
			$('#self').css('left', left);
		});
	});
	$('#rightbtn').on('touchend',function(e){
		e.preventDefault();
		clearInterval(selfTimer);
	});
});